package parse;

import java.util.ArrayList;
import java.util.List;
import model.HouseModel;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import model.JdModel;


public class JdParse {

  public static List<JdModel> getData(String html) throws Exception {
    //获取的数据，存放在集合中
    List<JdModel> data = new ArrayList<JdModel>();
    //采用Jsoup解析
    Document doc = Jsoup.parse(html);
    //获取html标签中的内容
    Elements elements = doc.select("ul[class=gl-warp clearfix]")
        .select("li[class=gl-item]");
    for (Element ele : elements) {
      String bookID = ele.attr("data-sku");
      String bookPrice = ele.select("div[class=p-price]").select("strong")
          .select("i").text();
      String bookName = ele.select("div[class=p-name]").select("em").text();
      //创建一个对象，这里可以看出，使用Model的优势，直接进行封装
      JdModel jdModel = new JdModel();
      //对象的值
      jdModel.setId(bookID);
      jdModel.setName(bookName);
      jdModel.setPrice(bookPrice);
      //将每一个对象的值，保存到List集合中
      data.add(jdModel);
    }
    //返回数据
    return data;
  }

  public static List<HouseModel> getData1(String html) throws Exception {
    //获取的数据，存放在集合中
    List<HouseModel> data = new ArrayList<HouseModel>();
    //采用Jsoup解析
    Document doc = Jsoup.parse(html);
    //获取html标签中的内容
    Elements elements = doc.select("div[class=left]");
    for (Element ele : elements) {
      String title = ele.select("div[class=infohead]").select("h1").text();
      String price = ele.select("div[class=infoform]").select("dl").select("dt")
          .select("dd:contains(元)").text();
      String type = ele.select("div[class=sitemap] a:nth-child(2)").text();
      String num = ele.select("div[class=infoform]").select("dl")
          .select("dt:contains(供应数量)").select("dd").text();
      String company = ele.select("div[class=infoform]").select("dl")
          .select("dt:contains(公司名称)").select("dd").text();
      String content = ele.select("div[class=introduce]").text();
      String person = ele.select("div[class=infocontact]").select("div")
          .select("ul").select("li:contains(联系人)").text();
      String phone = ele.select("div[class=infocontact]").select("div")
          .select("ul").select("li:contains(联系电话)").text();
      String address = ele.select("div[class=infocontact]").select("div")
          .select("ul").select("li:contains(联系地址)").text();
      String times = ele.select("div[class=infostatistics]").select("ul")
          .select("li:contains(发布)").text();
      String number = ele.select("div[class=infostatistics]").select("ul")
          .select("li:contains(浏览)").text();
      String mode = ele.select("div[class=infoform]").select("dl")
          .select("dt:contains(交易方式)").select("dd").text();
//      String bookPrice=ele.select("div[class=p-price]").select("strong").select("i").text();
//      String bookName=ele.select("div[class=p-name]").select("em").text();
      //创建一个对象，这里可以看出，使用Model的优势，直接进行封装
      HouseModel jdModel = new HouseModel();
      //对象的值
      jdModel.setAddress(address);
      jdModel.setCompany(company);
      jdModel.setContent(content);
      jdModel.setMode(mode);
      jdModel.setNum(num);
      jdModel.setNumber(number);
      jdModel.setPerson(person);
      jdModel.setPhone(phone);
      jdModel.setPrice(price);
      jdModel.setTimes(times);
      jdModel.setTitle(title);
      jdModel.setType(type);
      //将每一个对象的值，保存到List集合中
      data.add(jdModel);
    }
    //返回数据
    return data;
  }
}
